import { Tabs } from 'antd'
import { connect } from 'dva'

import AekLinkButton from '@/components/AekLinkButton'
import AekTable from '@/components/AekTable'
import PageLayout from '@/components/PageLayout'
import SearchForm from '@/components/SearchForm'
import { namespace } from '@/models/pcs/pcsStatementManage'
import { getPagination } from '@/utils/antdHelper'
import { formatMoneyNum } from '@/utils/displayHelper'
import { getBasicFn } from '@/utils/dvaUtil/index'
import { PCS_STATEMENT_STATUS_ENUM } from '@/utils/enum'

import { PcsStatementDetailDrawer } from './pcsStatementDetail'

/** 陪护 - 结算管理 */
const Index = ({ modelState, loading }) => {
  const { pageList, pagination, searchParams, totalNursingAmount } = modelState
  const { toAction, getLoading } = getBasicFn({ namespace, loading })

  const onActiveKeyChange = activeKey => {
    toAction({ searchParams: { status: activeKey } })
    toAction({ pagination: { current: 1 } }, true)
    toAction('getPageList')
  }

  const view = row => {
    PcsStatementDetailDrawer.PromiseThen({
      id: row.id,
    })
  }

  /** @type {import("@/components/SearchForm").SearchFormProps} */
  const searchProps = {
    searchParams,
    formData: [
      {
        field: 'statementNo',
        label: '结算单号',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'billNo',
        label: '账单号码',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'orderNo',
        label: '订单号码',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'patientName',
        label: '患者姓名',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'nursingWorkerName',
        label: '护工姓名',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'serviceDate',
        label: '服务日期',
        component: {
          name: 'RangePicker',
        },
      },
      {
        field: 'confirmDate',
        label: '确认日期',
        component: {
          name: 'RangePicker',
        },
      },
      {
        exclude: searchParams.status !== PCS_STATEMENT_STATUS_ENUM.COMPLETED,
        field: 'payDate',
        label: '支付日期',
        component: {
          name: 'RangePicker',
        },
      },
    ],
    onSearch: values => {
      toAction({ searchParams: values })
      toAction({ pagination: { current: 1 } }, true)
      toAction('getPageList')
    },
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey: 'id',
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    beforePagination: (
      <span>
        合计护工费用：
        <span className='c-money'>{formatMoneyNum(totalNursingAmount || 0)}</span>
      </span>
    ),
    columns: [
      {
        dataIndex: 'statementNo',
        title: '结算单号',
        render: (text, row) => <AekLinkButton onClick={() => view(row)}>{text}</AekLinkButton>,
      },
      {
        dataIndex: 'paySuccess',
        title: '支付成功',
        exclude: searchParams.status !== PCS_STATEMENT_STATUS_ENUM.COMPLETED,
        isMoney: true,
      },
      {
        dataIndex: 'payFail',
        title: '支付失败',
        exclude: searchParams.status !== PCS_STATEMENT_STATUS_ENUM.COMPLETED,
        isMoney: true,
      },
      {
        dataIndex: 'billTotalAmount',
        title: '账单总金额',
        exclude: searchParams.status === PCS_STATEMENT_STATUS_ENUM.COMPLETED,
        isMoney: true,
      },
      {
        dataIndex: 'payCommission',
        title: '账单支付手续费',
        exclude: searchParams.status === PCS_STATEMENT_STATUS_ENUM.COMPLETED,
        isMoney: true,
      },
      {
        dataIndex: 'nursingAmount',
        title: '护工费用',
        exclude: searchParams.status === PCS_STATEMENT_STATUS_ENUM.COMPLETED,
        isMoney: true,
      },
      { dataIndex: 'confirmUserName', title: '确认人' },
      { dataIndex: 'confirmTime', title: '确认时间', isDateTime: true },
      {
        dataIndex: 'payUserName',
        title: '支付人',
        exclude: searchParams.status !== PCS_STATEMENT_STATUS_ENUM.COMPLETED,
      },
      {
        dataIndex: 'payTime',
        title: '支付时间',
        isDateTime: true,
        exclude: searchParams.status !== PCS_STATEMENT_STATUS_ENUM.COMPLETED,
      },
      {
        key: 'operate',
        title: '操作',
        fixed: 'right',
        width: 100,
        className: 'text-center!',
        render: (_, row) => <AekLinkButton onClick={() => view(row)}>查看</AekLinkButton>,
      },
    ],
  }

  return (
    <PageLayout>
      <Tabs
        activeKey={String(searchParams.status)}
        items={PCS_STATEMENT_STATUS_ENUM.options.map(({ value, label }) => ({
          key: String(value),
          label,
        }))}
        onChange={onActiveKeyChange}
      />
      <SearchForm {...searchProps} />
      <AekTable {...tableProps} />
    </PageLayout>
  )
}

export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
